package com.test.controller;

import com.test.domain.Address;
import com.test.domain.Result;
import com.test.domain.Utils;
import com.test.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@RestController
@RequestMapping("/address")
public class AddressController {

    private final AddressService addressService;


    @Autowired
    public AddressController(AddressService addressService) {
        this.addressService = addressService;
    }


    @PostMapping("/insertAddress")
    public Result insertAddress(@RequestBody Address address){
        System.out.println(address);
        addressService.insertAddress(address);
        return Utils.success("ok");
    }

    @GetMapping("/addressList")
    public Result getAddressList(@RequestParam("phoneNumber") String phoneNumber){
        return Utils.success(addressService.getAddressListByPhoneNumber(phoneNumber));
    }

    @GetMapping("/updateName")
    public Result updateName(@RequestParam("AddressNumber")String addressNumber,
                             @RequestParam("name")String name){
        addressService.updateName(addressNumber,name);
        return Utils.success("ok");
    }

    @GetMapping("/updatePhoneNumber")
    public Result updatePhoneNumber(@RequestParam("AddressNumber")String addressNumber,
                             @RequestParam("phoneNumber")String phoneNumber){
        addressService.updatePhoneNumber(addressNumber,phoneNumber);
        return Utils.success("ok");
    }

    @GetMapping("/updateAddress")
    public Result updateAddress(@RequestParam("AddressNumber")String addressNumber,
                                    @RequestParam("address")String address){
        addressService.updateAddress(addressNumber,address);
        return Utils.success("ok");
    }

    @GetMapping("/delete")
    public Result deleteAddress(@RequestParam("AddressNumber")String addressNumber){
        addressService.deleteAddress(addressNumber);
        return Utils.success("ok");
    }

}
